<?php

class WebUser extends CWebUser
{
	private $_model;
	private $_place;
	
	/*
	public $identityCookie = array(
		'path' => '/',
		'domain' => '.pla2web.com',
	);
	
	public function init()
	{
		$conf = Yii::app()->session->cookieParams;
		$this->identityCookie = array(
			'path' => $conf['path'],
			'domain' => $conf['domain'],
		);
		parent::init();
	}
	*/
    public function login($identity, $duration = 0) {
    	$success = parent::login($identity, $duration);
    	$this->setModel($identity->getModel());
    	$this->setPlaceModel($this->getPlaceModel());
    	return $success;
    }
    
    /*
     * if not login return false
     * Setting language in database 
     */
    public function setLanguage($ln)
    {
    	if($this->isGuest)
    	{
    		return false;
    	}
    	$this->setState('lang',$ln);
    	$account = $this->loadModel();
    }
    
    public function getModel()
    {
    	if(isset($this->_model))
    	{
    		return $this->_model;
    	}
    	$this->_model = Account::model()->findByPK($this->id);
    	return $this->_model;
    }
    
    public function getPlaceModel()
    {
    	if(isset($this->_place))
    	{
    		return $this->_place;
    	}
    	$this->_place = Place::model()->findByAttributes(array('owner_id'=>$this->id));
    	return $this->_place;
    }
    
	public function refreshModel()
    {
    	$this->setModel($this->getModel());
    }
    
    public function refreshPlaceModel()
    {
    	$this->setPlaceModel($this->getPlaceModel());
    }
    
    private function setModel($model)
    {
    	$this->_model = $model;
    	if($this->_model != null){
    		$userInfo = $this->_model->attributes;
    		unset($userInfo['password']);
	    	$this->setState('userInfo',$userInfo);
	    	$this->setState('lang',$this->_model['lang']);
    	}
    }
    
    private function setPlaceModel($model)
    {
    	$this->_place = $model;
    	if($this->_place != null){
    		$this->setState('placeInfo',$this->_place->attributes);
    	}
    }
}
